//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <ddk.h>
#include <stdio.h>
#include "CDeviceEventManager.h"

static IDeviceEventHandler *s_pDeviceEventHandler = NULL;
static IDeviceEventManager *s_pDeviceEventManager = NULL;
static IStub *s_pStub = NULL;

class CEventManDev : public CDeviceDriver
{
public:
    CARAPI Read(
        /* [in] */ Int64 u64Offset,
        /* [in] */ Int32 bytesToRead,
        /* [out] */ MemoryBuf * pBuffer,
        /* [out] */ IEvent * * ppCompletionEvent);

    CARAPI Write(
        /* [in] */ Int64 u64Offset,
        /* [in] */ const MemoryBuf & buffer,
        /* [out] */ Int32 * pBytesWritten,
        /* [out] */ IEvent * * ppCompletionEvent);

    CARAPI Control(
        /* [in] */ Handle32 nControlCode,
        /* [in] */ const MemoryBuf & inBuffer,
        /* [out] */ MemoryBuf * pOutBuffer,
        /* [out] */ IEvent * * ppCompletionEvent);

    virtual void Dispose();
};

EXTERN IDeviceDriver * CDECL RegisterEventManager(
                uint_t uDeviceNo, void *pvParameter)
{
    CEventManDev *pStatServer = new CEventManDev;
    if (NULL == pStatServer) {
        return NULL;
    }
    pStatServer->AddRef();

    CDeviceEventManager *pem = new CDeviceEventManager();
    if (NULL == pem) {
        delete pStatServer;
        return NULL;
    }
    ECode ec = pem->Initialize();
    if (FAILED(ec)) {
        delete pStatServer;
        return NULL;
    }
    s_pDeviceEventHandler = (IDeviceEventHandler *)pem;
    s_pDeviceEventHandler->AddRef();

    ec = DzRegisterService(L"DeviceEventManager", s_pDeviceEventHandler, &s_pStub);
    if (FAILED(ec)) {
        delete pStatServer;
        s_pDeviceEventHandler->Release();
        kprintf("Register DeviceEventManager failed, ec = 0x%08x\n", ec);
        return NULL;
    }
    assert(s_pStub != NULL);

    s_pDeviceEventManager = (IDeviceEventManager *)pem;
    s_pDeviceEventManager->AddRef();

    return pStatServer;
}

ECode CEventManDev::Read(
    /* [in] */ Int64 u64Offset,
    /* [in] */ Int32 bytesToRead,
    /* [out] */ MemoryBuf * pBuffer,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    return E_NOT_IMPLEMENTED;
}

ECode CEventManDev::Write(
    /* [in] */ Int64 u64Offset,
    /* [in] */ const MemoryBuf & buffer,
    /* [out] */ Int32 * pBytesWritten,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    return E_NOT_IMPLEMENTED;
}

ECode CEventManDev::Control(
    /* [in] */ Handle32 nControlCode,
    /* [in] */ const MemoryBuf & inBuffer,
    /* [out] */ MemoryBuf * pOutBuffer,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    return E_NOT_IMPLEMENTED;
}

void CEventManDev::Dispose()
{
    if (s_pDeviceEventHandler) s_pDeviceEventHandler->Release();
    if (s_pDeviceEventManager) s_pDeviceEventManager->Release();
    ECode ec = DzUnregisterService(L"DeviceEventManager", s_pStub);
    if (FAILED(ec)) {
        kprintf("Failed to unregister DeviceEventManager service!\n");
    }

    delete this;
}

EXTERN ECode EmSendEvent(
    /* [in] */ UInt32 uEventNum,
    /* [in] */ const MemoryBuf & ebEventData)
{
    return s_pDeviceEventManager->SendEvent(uEventNum, ebEventData);
}

EXTERN ECode EmPostEvent(
    /* [in] */ UInt32 uEventNum,
    /* [in] */ const MemoryBuf & ebEventData)
{
    return s_pDeviceEventManager->PostEvent(uEventNum, ebEventData);
}
